 
package com.zdkj.subway.rca.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.rca.entity.SwCkStatementCatXaztReplenishTmp;
import com.zdkj.subway.rca.entity.SwCkStatementCatXaztReplenish;
import com.zdkj.subway.rca.mapper.SwCkStatementCatXaztReplenishTmpMapper;
import com.zdkj.subway.rca.service.SwCkStatementCatXaztReplenishTmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.ZoneId;
import java.util.*;

/**
 * 长安通卡充资对账确认临时表（西安中铁-西安地铁）
 *
 * @author stw
 * @date 2020-10-24 18:03:22
 */
@Service
public class SwCkStatementCatXaztReplenishTmpServiceImpl extends ServiceImpl<SwCkStatementCatXaztReplenishTmpMapper, SwCkStatementCatXaztReplenishTmp> implements SwCkStatementCatXaztReplenishTmpService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SwCkStatementCatXaztReplenishTmpServiceImpl.class);
    private String parsePattern = "yyyy.MM.dd";
    @Autowired
    private com.zdkj.subway.rca.service.SwCkStatementCatXaztReplenishService swCkStatementCatXaztReplenishService;

    @Override
    public Boolean saveBatchWithExcel(final File excelFile) throws Exception {
        List<Map<Integer, String>> listMap = EasyExcel.read(excelFile).sheet().headRowNumber(6).doReadSync();
        if (CollectionUtil.isNotEmpty(listMap)) {
            List<SwCkStatementCatXaztReplenishTmp> listDb = new ArrayList<>();
            MultipartFileToFile.delteTempFile(excelFile);
            for (int m = 0; m < listMap.size(); m++) {
                Map<Integer, String> data = listMap.get(m);
                // 返回每条数据的键值对 表示所在的列 和所在列的值
                LOGGER.info("读取到数据:{}", JSON.toJSONString(data));

                SwCkStatementCatXaztReplenishTmp tmpObj = new SwCkStatementCatXaztReplenishTmp();
                tmpObj.setTradeDateTmp(data.get(0));//交易日期

                tmpObj.setEleXasubwayAmountTmp(data.get(1));
                tmpObj.setEleXasubwayMoneyTmp(data.get(2));
                tmpObj.setCashXasubwayAmountTmp(data.get(3));
                tmpObj.setCashXasubwayMoneyTmp(data.get(4));

				tmpObj.setEleXaztAmountTmp(data.get(5));
                tmpObj.setEleXaztMoneyTmp(data.get(6));
                tmpObj.setCashXaztAmountTmp(data.get(7));
                tmpObj.setCashXaztMoneyTmp(data.get(8));

				tmpObj.setEleAirAmountTmp(data.get(9));
                tmpObj.setEleAirMoneyTmp(data.get(10));
                tmpObj.setCashAirAmountTmp(data.get(11));
                tmpObj.setCashAirMoneyTmp(data.get(12));

			    tmpObj.setTotalXasubwayAmountTmp(data.get(13));
                tmpObj.setTotalXasubwayMoneyTmp(data.get(14));

				tmpObj.setTotalXaztAmountTmp(data.get(15));
                tmpObj.setTotalXaztMoneyTmp(data.get(16));  
				
				tmpObj.setTotalAirAmountTmp(data.get(17));
                tmpObj.setTotalAirMoneyTmp(data.get(18));  
              
                
                tmpObj.setTotalAmountTmp(data.get(19));
                tmpObj.setTotalMoneyTmp(data.get(20));
               

                if (StringUtils.isEmpty(tmpObj.getTradeDateTmp())
                        || StringUtils.isEmpty(tmpObj.getTotalAmountTmp())
                        || StringUtils.isEmpty(tmpObj.getTotalMoneyTmp())
                        ) {
                    continue;
                }
                if (!parseDate(tmpObj.getTradeDateTmp())) {
                    continue;
                }

                final SwCkStatementCatXaztReplenishTmp obj = ObjectUtils.getDefaultObj(SwCkStatementCatXaztReplenishTmp.class);
                BeanUtils.copyProperties(tmpObj, obj, "deptId", "creatorId", "creatorName");
                checkErr(obj);
                listDb.add(obj);

            }
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("CREATOR_ID", listDb.get(0).getCreatorId());
            this.baseMapper.deleteByMap(columnMap);
            return this.saveBatch(listDb);

        }

        return false;
    }

    private void checkErr(SwCkStatementCatXaztReplenishTmp obj) {
        //更新字段
        String tradeDateTmp = obj.getTradeDateTmp();
        try {
            Date tradeDate = DateUtils.parseDate(tradeDateTmp, parsePattern);
            obj.setTradeDate(tradeDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        } catch (Exception e) {
            obj.setErrMsg("交易日期 为空或格式错误");
        }

        try {
            String eleXasubwayAmountTmp = obj.getEleXasubwayAmountTmp();
            Long eleXasubwayAmount = Long.parseLong(eleXasubwayAmountTmp);
            obj.setEleXasubwayAmount(eleXasubwayAmount);

           
			String cashXasubwayAmountTmp = obj.getCashXasubwayAmountTmp();
            Long cashXasubwayAmount = Long.parseLong(cashXasubwayAmountTmp);
            obj.setCashXasubwayAmount(cashXasubwayAmount);

         	String eleXaztAmountTmp = obj.getEleXaztAmountTmp();
            Long eleXaztAmount = Long.parseLong(eleXaztAmountTmp);
            obj.setEleXaztAmount(eleXaztAmount);

            
			String cashXaztAmountTmp = obj.getCashXaztAmountTmp();
            Long cashXaztAmount = Long.parseLong(cashXaztAmountTmp);
            obj.setCashXaztAmount(cashXaztAmount);

			//
			String eleAirAmountTmp = obj.getEleAirAmountTmp();
            Long eleAirAmount = Long.parseLong(eleAirAmountTmp);
            obj.setEleAirAmount(eleAirAmount);

            
			String cashAirAmountTmp = obj.getCashAirAmountTmp();
            Long cashAirAmount = Long.parseLong(cashAirAmountTmp);
            obj.setCashAirAmount(cashAirAmount);
			//

			String totalXasubwayAmountTmp = obj.getTotalXasubwayAmountTmp();
            Long totalXasubwayAmount = Long.parseLong(totalXasubwayAmountTmp);
            obj.setTotalXasubwayAmount(totalXasubwayAmount);

         	String totalXaztAmountTmp = obj.getTotalXaztAmountTmp();
            Long totalXaztAmount = Long.parseLong(totalXaztAmountTmp);
            obj.setTotalXaztAmount(totalXaztAmount);      
			
			String totalAirAmountTmp = obj.getTotalAirAmountTmp();
            Long totalAirAmount = Long.parseLong(totalAirAmountTmp);
            obj.setTotalAirAmount(totalAirAmount);      
            

            String totalAmountTmp = obj.getTotalAmountTmp();
            Long totalAmount = Long.parseLong(totalAmountTmp);
            obj.setTotalAmount(totalAmount);
           
        } catch (Exception e) {
            obj.setErrMsg("笔数 为空或格式错误");
        }

        try {
            BigDecimal num = new BigDecimal("100");

            String eleXasubwayMoneyTmp = obj.getEleXasubwayMoneyTmp();
            BigDecimal eleXasubwayMoney = new BigDecimal(eleXasubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            eleXasubwayMoney = eleXasubwayMoney.multiply(num);
            obj.setEleXasubwayMoney(eleXasubwayMoney.longValue());

			String cashXasubwayMoneyTmp = obj.getCashXasubwayMoneyTmp();
            BigDecimal cashXasubwayMoney = new BigDecimal(cashXasubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            cashXasubwayMoney = cashXasubwayMoney.multiply(num);
            obj.setCashXasubwayMoney(cashXasubwayMoney.longValue());

			String eleXaztMoneyTmp = obj.getEleXaztMoneyTmp();
            BigDecimal eleXaztMoney = new BigDecimal(eleXaztMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            eleXaztMoney = eleXaztMoney.multiply(num);
            obj.setEleXaztMoney(eleXaztMoney.longValue());

			String cashXaztMoneyTmp = obj.getCashXaztMoneyTmp();
            BigDecimal cashXaztMoney = new BigDecimal(cashXaztMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            cashXaztMoney = cashXaztMoney.multiply(num);
            obj.setCashXaztMoney(cashXaztMoney.longValue());
            ////
			String eleAirMoneyTmp = obj.getEleAirMoneyTmp();
            BigDecimal eleAirMoney = new BigDecimal(eleAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            eleAirMoney = eleAirMoney.multiply(num);
            obj.setEleAirMoney(eleAirMoney.longValue());

			String cashAirMoneyTmp = obj.getCashAirMoneyTmp();
            BigDecimal cashAirMoney = new BigDecimal(cashAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            cashAirMoney = cashAirMoney.multiply(num);
            obj.setCashAirMoney(cashAirMoney.longValue());
			///

			String totalXasubwayMoneyTmp = obj.getTotalXasubwayMoneyTmp();
            BigDecimal totalXasubwayMoney = new BigDecimal(totalXasubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalXasubwayMoney = totalXasubwayMoney.multiply(num);
            obj.setTotalXasubwayMoney(totalXasubwayMoney.longValue());

			
			String totalXaztMoneyTmp = obj.getTotalXaztMoneyTmp();
            BigDecimal totalXaztMoney = new BigDecimal(totalXaztMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalXaztMoney = totalXaztMoney.multiply(num);
            obj.setTotalXaztMoney(totalXaztMoney.longValue());

			String totalAirMoneyTmp = obj.getTotalAirMoneyTmp();
            BigDecimal totalAirMoney = new BigDecimal(totalAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalAirMoney = totalAirMoney.multiply(num);
            obj.setTotalAirMoney(totalAirMoney.longValue());


          
            String totalMoneyTmp = obj.getTotalMoneyTmp();
            BigDecimal totalMoney = new BigDecimal(totalMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalMoney = totalMoney.multiply(num);
            obj.setTotalMoney(totalMoney.longValue());
          
        } catch (Exception e) {
            obj.setErrMsg("金额 为空或格式错误");
        }


        //判断数据是否存在
        SwCkStatementCatXaztReplenish SwCkStatementCatXaztReplenish = new SwCkStatementCatXaztReplenish();
        BeanUtils.copyProperties(obj, SwCkStatementCatXaztReplenish, "deptId","creatorId","creatorName","createTime");
        List<SwCkStatementCatXaztReplenish> list = swCkStatementCatXaztReplenishService.list(Wrappers.query(SwCkStatementCatXaztReplenish));
        if (list != null && !list.isEmpty()) {
            obj.setErrMsg("数据已存在");
        }
    }

    private boolean parseDate(String string) {
        boolean convertSuccess = true;
        if (string == null) {
            return false;
        }

        try {
            DateUtils.parseDate(string, parsePattern);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    @Override
    public void saveBatchWithExcelOk(Integer creatorId) {
        //导入先删除
        this.baseMapper.deleteBatchWithExcelOk(creatorId);
        //导入先删除再添加包括重复的
        this.baseMapper.saveBatchWithExcelOkRepeat(creatorId);
        //this.baseMapper.saveBatchWithExcelOk(creatorId);
        saveBatchWithExcelCancel(creatorId);
    }

    @Override
    public void saveBatchWithExcelCancel(Integer creatorId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("CREATOR_ID", creatorId);
        this.baseMapper.deleteByMap(columnMap);
    }
}
